package community;

import java.util.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;

import util.*;
import board.*;

public class CommunityGo extends Action{
//커뮤니티 선택하였을 때 실행되는 메소드	
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		CommunityForm coForm = (CommunityForm)form;
		
		CommunityDAO coDAO = new CommunityDAO();
		MemberCommunityDAO mcDAO = new MemberCommunityDAO();
		
		FreeBoardAction fbAction = new FreeBoardAction();
		DataBoardAction dbAction = new DataBoardAction();
		PhotoBoardAction pbAction = new PhotoBoardAction();
		
		int cono = Integer.parseInt(request.getParameter("cono"));
		
		//공지사항 최신 목록 가져오기
		NoticeBoardDAO nbDAO = new NoticeBoardDAO();
		coForm.setNoticeBoard(nbDAO.getNewContent(cono));
		
		//선택한 커뮤니티에 해당하는 정보 가져오기 및 셋팅
		Community community = new Community();
		community.setCono(cono);
		community = coDAO.getCommunity(community);
		coForm.setCono(community.getCono());
		coForm.setConame(community.getConame());
		coForm.setCoopen(community.getCoopen());
		coForm.setCoregdate(community.getCoregdate());
		coForm.setCologoofn(community.getCologoofn());
		coForm.setCologosfn(community.getCologosfn());
		coForm.setCopoptext(community.getCopoptext());
		coForm.setCopopwidth(community.getCopopwidth());
		coForm.setCopopheight(community.getCopopheight());
		coForm.setCoguide(community.getCoguide());
		coForm.setCcno(community.getCcno());
		coForm.setCount(mcDAO.countCommunityMembers(coForm.getCono()));
		coForm.setMcnick(mcDAO.getCommunityMcnick(cono));
		coForm.setMid(mcDAO.getCommunityMid(cono));		

		this.setMemberCommunity(request);
/*		Menu menu = new Menu();
		
		menu.setCommunity(community);
		
		//공지사항 셋팅
		BoardNames bn = new BoardNames();
		bn.setBname("공지사항");
		bn.setUrl("noticeboard.do?method=getList&cono="+cono+"&nbname=");
		Vector<BoardNames> vtbn = new Vector<BoardNames>();
		vtbn.add(bn);
		menu.setNoticeBoardMenus(vtbn);
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		//게시판 셋팅
		menu.setFreeBoardMenus(fbAction.getFreeboardMenus(cono));
		menu.setDataBoardMenus(dbAction.getDataboardMenus(cono));
		menu.setPhotoBoardMenus(pbAction.getPhotoboardMenus(cono));
		
		//커뮤니티 번호 셋팅
		menu.setCono(cono);
		
		//커뮤니티 링크 셋팅
		CommunityLinkDAO clDAO = new CommunityLinkDAO();
		menu.setCommLinkMenus(clDAO.getCommunityLinks(cono));
		
		//가입한 커뮤니티 목록 추가
		if(session.getAttribute("user")!=null){
			if(user.isLogon()){
				menu.setMemberCommunitys(mcDAO.getMemberCommunity(user.getMid()));
			}else{
				menu.setMemberCommunitys(mcDAO.getMemberCommunity());
			}
		}
			
		request.getSession().setAttribute("menu", menu);

		
		MemberCommunity mc = new MemberCommunity();
		
		user.setMcnick(mcDAO.getCommunityNick(user.getMid(), cono));
		session.setAttribute("user", user);		
	
		
		mc.setMid(user.getMid());
		mc.setCono(coForm.getCono());
		mc = mcDAO.getMemberCommunity(mc);
		
		request.getSession().removeAttribute("membercommunity");
		request.getSession().setAttribute("membercommunity", mc);		*/
			
		return mapping.findForward("ok");
	}

//메뉴 구성하는 메소드
	public static void setMemberCommunity(HttpServletRequest request){
	
		int cono = Integer.parseInt(request.getParameter("cono"));
		
		CommunityDAO coDAO = new CommunityDAO();
		MemberCommunityDAO mcDAO = new MemberCommunityDAO();
		
		FreeBoardAction fbAction = new FreeBoardAction();
		DataBoardAction dbAction = new DataBoardAction();
		PhotoBoardAction pbAction = new PhotoBoardAction();
		
		Community community = new Community();
		
		community.setCono(cono);
		community = coDAO.getCommunity(community);
		
		Menu menu = new Menu();
		
		menu.setCommunity(community);
		
		//공지사항 셋팅
		BoardNames bn = new BoardNames();
		bn.setBname("공지사항");
		bn.setUrl("noticeboard.do?method=getList&cono="+cono+"&nbname=");
		Vector<BoardNames> vtbn = new Vector<BoardNames>();
		vtbn.add(bn);
		menu.setNoticeBoardMenus(vtbn);
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");

		//게시판 셋팅
		menu.setFreeBoardMenus(fbAction.getFreeboardMenus(cono));
		menu.setDataBoardMenus(dbAction.getDataboardMenus(cono));
		menu.setPhotoBoardMenus(pbAction.getPhotoboardMenus(cono));
		
		//커뮤니티 번호 셋팅
		menu.setCono(cono);
		
		//커뮤니티 링크 셋팅
		CommunityLinkDAO clDAO = new CommunityLinkDAO();
		menu.setCommLinkMenus(clDAO.getCommunityLinks(cono));
		
		//가입한 커뮤니티 목록 추가
		if(session.getAttribute("user")!=null){
			if(user.isLogon()){
				menu.setMemberCommunitys(mcDAO.getMemberCommunity(user.getMid()));
			}else{
				menu.setMemberCommunitys(mcDAO.getMemberCommunity());
			}
		}
			
		request.getSession().setAttribute("menu", menu);

		
		MemberCommunity mc = new MemberCommunity();
		
		user.setMcnick(mcDAO.getCommunityNick(user.getMid(), cono));
		session.setAttribute("user", user);		
	
		
		mc.setMid(user.getMid());
		mc.setCono(cono);
		mc = mcDAO.getMemberCommunity(mc);
		
		request.getSession().removeAttribute("membercommunity");
		request.getSession().setAttribute("membercommunity", mc);		
	}

	
}
